/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filesTableModel;

import SessionBeans.web.beans.FileInfo;
import SessionBeans.web.FilesTableSessionBean;
import SessionBeans.web.beans.FolderInfo;
import entities.File;
import entities.FileBlob;
import entities.Folder;
import entities.User;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Tomas
 */
public class FilesTableModel {

    FilesTableSessionBean filesTableSessionBean = lookupFilesTableSessionBeanBean();

    public FilesTableModel() {
    }

    public TableItemInterface loadFiles(Folder folder, Long userId) {
         return loadFiles(folder.getId(), userId);
    }

    public TableItemInterface loadFiles(Long folderId, Long userId) {
        FolderInfo loadFiles = filesTableSessionBean.loadFiles(folderId, userId);
        return buildFiles(loadFiles);
    }

    private TableItemInterface buildFiles(FolderInfo folderInfo) {
        List<TableItemInterface> childs = new ArrayList<>();

        for (FolderInfo childFolder : folderInfo.getFolders()) {
            childs.add(new TableItemFolder2(childFolder));
        }
        for (FileInfo file : folderInfo.getFiles()) {
            childs.add(new TableItemFile(file));
        }

        return new TableItemFolder2(folderInfo, childs);
    }

    public TableItemInterface loadUserFile(User user) {
        FolderInfo loadUserFiles = filesTableSessionBean.loadUserFiles(user.getId());
        
        return buildFiles(loadUserFiles);
    }
    
    public FileBlob loadFileValue(Long fileBlobId){
        return filesTableSessionBean.loadFileValue(fileBlobId);
    }
    
    public FileBlob loadFileValue(File file){
        return loadFileValue(file.getFileBlobId());
    }
    
    public TableItemInterface createFolder(User user, Folder parentFolder, String name){
        FolderInfo createFolder = filesTableSessionBean.createFolder(user.getId(), parentFolder.getId(), name);
        
        return buildFiles(createFolder);
    }
    
    public TableItemInterface createFile(User user, Folder parentFolder, String name, byte[] value){
        FolderInfo folder = filesTableSessionBean.uploadFile(user.getId(), parentFolder.getId(), name, value);
        
        return buildFiles(folder);
    }

    private FilesTableSessionBean lookupFilesTableSessionBeanBean() {
        try {
            Context c = new InitialContext();
            return (FilesTableSessionBean) c.lookup("java:global/tCloudEE/tCloudEE-ejb/FilesTableSessionBean!SessionBeans.web.FilesTableSessionBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
